(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.childNodeEditController', childNodeEditController);
	childNodeEditController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService', 'ajaxServiceFactory' ];
	function childNodeEditController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService, ajaxServiceFactory) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("childNodeEditPopup"), ajaxService = ajaxServiceFactory.buildAjaxService({
		});
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.childLimitNodes = ctrl.dialogsModule.params.childLimitNodes || [];
		ctrl.childNode = {};
		ctrl.formName = "childNodeForm";
		
		ctrl.init = function() {
			ctrl.limitNodeIdOptions = {'data' : []};
			if (ctrl.childLimitNodes) {
				for (var i = 0; i < ctrl.childLimitNodes.length; i++) {
					var childLimitNode = ctrl.childLimitNodes[i];
					var option = {};
					option.value = childLimitNode.id;
					option.label = childLimitNode.label;
					ctrl.limitNodeIdOptions.data.push(option);
				}
			}
		};
		
		ctrl.ok = function() {
			if (!ctrl.childNode.limitNodeId) {
				modalService.openErrorDialogs({
					messages : [ 'please select Limit Node first!' ]
				});
				return;
			}
			if ($scope.childNodeForm) {
				$scope.childNodeForm.$setSubmitted();
			}
			if ((!$scope.childNodeForm || $scope.childNodeForm.$valid) && (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.childNode) !== false)) {
				$uibModalInstance.close();
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
	}
})(angular);